package com.aier.admin_new.common;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Enumeration;
import java.util.zip.ZipException;

import org.apache.commons.io.FileUtils;
import org.apache.tools.ant.Project;
import org.apache.tools.ant.taskdefs.Zip;
import org.apache.tools.ant.types.FileSet;
import org.apache.tools.zip.ZipEntry;
import org.apache.tools.zip.ZipFile;


import com.aier.common.common.ImageUtil;
import com.aier.common.common.SystemConfigStatic;


/**
 * 压缩和解压zip
 * @author Wkk300
 */


public class CompresszZipFile {  
      
	 /**
	  * 解压缩
	  * @param zipFileName 指定压缩文件
	  * @param destDir 指定解压目录
	  * @throws Exception
	  */
	 public static void unzip(File zipFileName, String destDir) {
	  try {
	   ZipFile zipFile = new ZipFile(zipFileName);
	   Enumeration<?> e = zipFile.getEntries();
	   ZipEntry zipEntry = null;
	   File fD = new File(destDir);
	   if(!fD.exists()){
	    fD.mkdir();
	   }
	   while (e.hasMoreElements()) {
	    zipEntry = (ZipEntry) e.nextElement();
	    String entryName = zipEntry.getName();
	    String names[] = entryName.split("/");
	    int length = names.length;
	    String path = destDir;
	    for (int v = 0; v < length; v++) {
	     if (v < length - 1) {
	      path += "/" + names[v];
	      new File(path).mkdir();
	     } else { 
	      if (entryName.endsWith("/")) { 
	       new File(destDir + "/" + entryName).mkdir();
	      } else {
	       InputStream in = zipFile.getInputStream(zipEntry);
	       OutputStream os = new FileOutputStream(new File(destDir + "/" + entryName));
	       byte[] buf = new byte[1024];
	       int len;
	       while ((len = in.read(buf)) > 0) {
	        os.write(buf, 0, len);
	       }
	       in.close();
	       os.close();
	      }
	     }
	    }
	   }
	   zipFile.close();
	  } catch (Exception ex) {
	   ex.printStackTrace();
	  }
	 }
	 
	 /**
	  * 压缩
	  * @param targetZip 目标ZIP
	  * @param sourceFile 源文件
	  */
	 public static void zip(String targetZip, String sourceFile){
	  Project prj = new Project();
	  Zip zip = new Zip();
	  zip.setProject(prj);
	  zip.setDestFile(new File(targetZip));
	  FileSet fileSet = new FileSet();
	  fileSet.setProject(prj);
	  fileSet.setDir(new File(sourceFile));
	  zip.addFileset(fileSet);
	  zip.execute();
	 }
	 
	 
	 /**
	  * 
	  */
	 
	 public static String uploadDailyfoodImgApp(File file,String photoName,String imgUrl) {
	        String picName = "";
	        String folderPath =SystemConfigStatic.temp_upload_url;
	        try {      
	        	String zippath=folderPath+Math.random()+"zip";// 根据路径名创建一个放zip的文件夹
	    		File filea = new File(zippath);
	    		if(!filea.exists()){
	    			filea.mkdir();
	    		}
	    		
	        	FileUtils.copyFile(file, new File(filea, photoName));
	        	File fileazip = new File(filea+"/"+photoName);  //路径zip文件名
	        	String zipImgpath = folderPath+Math.random()+"jpg";//存放图片的文件夹
	    		File fileb = new File(zipImgpath);	//解压到某个文件夹下
	    		 if(fileb.exists()){
	    			 fileb.mkdir();
	    		 }
	    		 
	    		 CompresszZipFile.unzip(fileazip, zipImgpath);
	    		 
				//读取这个文件夹下的所有图片
				 File f = new File(zipImgpath);
				 File [] files = f.listFiles();
		         for (int i = 0; i < files.length; i++){
		             File file1 = files[i];
		             String fileName = ImageUtil.uploadImage(file1,imgUrl, file1.getName(),(int) 1);
		             if (!"".equals(picName)) {
	                   picName += ",";
	              }
		             picName += fileName;
		        } 
		       //删除这个zip文件夹
		        FileToolkit.deleteFile(zipImgpath);
				FileToolkit.deleteFile(zippath);
			} catch (ZipException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}
	        return picName;
	    }
}  
